package com.ilarele.shopping.client.adaptors;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ilarele.shopping.Product;
import com.ilarele.shopping.client.R;
import com.ilarele.shopping.client.util.DrawableManager;

public class A2SimpleProductAdapter extends ArrayAdapter<Product> {
	private final LayoutInflater mLayoutInflater;

	public A2SimpleProductAdapter(Activity activity, ArrayList<Product> items) {
		super(activity, R.layout.a1_shoppinglist_item, items);
		mLayoutInflater = LayoutInflater.from(activity);
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;

		if (view == null) {
			view = mLayoutInflater.inflate(R.layout.a1_shoppinglist_item,
					parent, false);

			holder = new ViewHolder();
			holder.name = (TextView) view.findViewById(R.id.list_item_name);
			holder.picture = (ImageView) view
					.findViewById(R.id.list_item_picture);

			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		Product item = getItem(position);
		holder.name.setText(item.pName.name);
		DrawableManager.fetchView(item.pPicture.imageUrl, holder.picture);
		return view;
	}

	private static class ViewHolder {
		TextView name;
		ImageView picture;
	}
}
